package cn.tedu.powerfulfitness.service.impl;

import cn.tedu.powerfulfitness.ex.ServiceException;
import cn.tedu.powerfulfitness.mapper.UserMapper;
import cn.tedu.powerfulfitness.pojo.dto.UserAddNewDTO;
import cn.tedu.powerfulfitness.pojo.dto.UserUpdateDTO;
import cn.tedu.powerfulfitness.pojo.entity.User;
import cn.tedu.powerfulfitness.pojo.vo.UserListItemVO;
import cn.tedu.powerfulfitness.pojo.vo.UserStandardVO;
import cn.tedu.powerfulfitness.service.IUserService;
import cn.tedu.powerfulfitness.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public void addNew(UserAddNewDTO userAddNewDTO) {
        // 从参数对象中获取相册名称
        String name = userAddNewDTO.getUsername();
        // 调用userMapper的int countByName(String name)根据名称执行统计
        int countByName = userMapper.countByName(name);
        // 判断统计结果是否大于0
        if (countByName > 0) {
            // 是：抛出异常
            String message = "添加用户名失败，用户名已经被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 创建User对象
        User user = new User();
        // 调用BeanUtils.copyProperties()将参数对象的属性值复制到User对象中
        BeanUtils.copyProperties(userAddNewDTO, user);
        // 调用userMapper的int insert(User User)方法将相册数据插入到数据库
        int rows = userMapper.insert(user);
        if (rows != 1) {
            String message = "添加用户名，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Integer id) {
        UserStandardVO queryResult = userMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "删除用户失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int rows = userMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除用户失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Integer id, UserUpdateDTO userUpdateDTO) {
        UserStandardVO queryResult = userMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "修改用户详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        String name = userUpdateDTO.getUsername();
        int count = userMapper.countByNameAndNotId(id, name);
        if (count > 0) {
            String message = "修改用户详情失败，相册名称已经被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO, user);
        user.setId(id);
        int rows = userMapper.update(user);
        if (rows != 1) {
            String message = "修改用户详情失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public UserStandardVO getStandardById(Integer id) {
        UserStandardVO queryResult = userMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "获取用户详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        return queryResult;
    }

    @Override
    public List<UserListItemVO> list() {
        List<UserListItemVO> list = userMapper.list();
        return list;
    }

}
